import { Module, Global } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { LoggingService } from './logging.service';
import {
  LoggingInterface,
  StructuredLoggingInterface,
} from './logging.interface';

/**
 * 日志模块
 * 基于 NestJS 官方示例实现
 * 参考: https://docs.nestjs.cn/fundamentals/dependency-injection
 * 对应 Java: 日志配置
 */
@Global()
@Module({
  imports: [ConfigModule],
  providers: [
    {
      provide: 'LOGGING_PROVIDER',
      useFactory: (configService: ConfigService) => {
        // 这里会根据配置选择具体的日志实现
        // 默认使用 Winston，也可以使用其他日志库
        const loggingType = configService.get('logging.type', 'winston');

        if (loggingType === 'winston') {
          // 返回 Winston 日志实现
          return {
            async log(
              level: string,
              message: string,
              context?: any,
            ): Promise<void> {
              console.log(`[${level.toUpperCase()}] ${message}`, context || '');
            },
            async error(message: string, context?: any): Promise<void> {
              console.error(`[ERROR] ${message}`, context || '');
            },
            async warn(message: string, context?: any): Promise<void> {
              console.warn(`[WARN] ${message}`, context || '');
            },
            async info(message: string, context?: any): Promise<void> {
              console.info(`[INFO] ${message}`, context || '');
            },
            async debug(message: string, context?: any): Promise<void> {
              console.debug(`[DEBUG] ${message}`, context || '');
            },
          };
        } else {
          // 返回其他日志实现
          return {
            async log(
              level: string,
              message: string,
              context?: any,
            ): Promise<void> {
              console.log(`[${level.toUpperCase()}] ${message}`, context || '');
            },
            async error(message: string, context?: any): Promise<void> {
              console.error(`[ERROR] ${message}`, context || '');
            },
            async warn(message: string, context?: any): Promise<void> {
              console.warn(`[WARN] ${message}`, context || '');
            },
            async info(message: string, context?: any): Promise<void> {
              console.info(`[INFO] ${message}`, context || '');
            },
            async debug(message: string, context?: any): Promise<void> {
              console.debug(`[DEBUG] ${message}`, context || '');
            },
          };
        }
      },
      inject: [ConfigService],
    },
    {
      provide: 'STRUCTURED_LOGGING_PROVIDER',
      useFactory: (configService: ConfigService) => {
        // 结构化日志实现
        return {
          async logStructured(
            level: string,
            message: string,
            metadata: any,
          ): Promise<void> {
            const structuredLog = {
              timestamp: new Date().toISOString(),
              level,
              message,
              metadata,
            };
            console.log(JSON.stringify(structuredLog));
          },
          async logError(error: Error, context?: any): Promise<void> {
            const structuredLog = {
              timestamp: new Date().toISOString(),
              level: 'error',
              message: error.message,
              stack: error.stack,
              context,
            };
            console.error(JSON.stringify(structuredLog));
          },
          async logPerformance(
            operation: string,
            duration: number,
            metadata?: any,
          ): Promise<void> {
            const structuredLog = {
              timestamp: new Date().toISOString(),
              level: 'info',
              message: `Performance: ${operation}`,
              duration,
              metadata,
            };
            console.log(JSON.stringify(structuredLog));
          },
        };
      },
      inject: [ConfigService],
    },
    LoggingService,
  ],
  exports: [LoggingService],
})
export class LoggingModule {}
